<?php
/**
 * Created by PhpStorm.
 * Admin: Administrator
 * Date: 2018/6/6/006
 * Time: 9:32
 */

namespace App\Http\Controllers\Account;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Service\Account\AccountService;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\Redis;
use Session,Config;
use Cache;
use Excel;

class IndexController extends Controller
{
    /**
     * @return view
     * 会员列表页面
     */
    public function index()
    {
        return view('Account.account_list');
    }

    /**
     * @param Request $request
     * 用户账号列表
     */
    public function getAccountList(Request $request)
    {

        $where= $request->except('_token');

        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }


        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $Account = new AccountService();
        $data = $Account->getAccountList($where);

        if($data && !empty($data['data']))
        {
            foreach ($data['data'] as &$value)
            {
                $value['wechat_bind_name'] = $value['wechat_bind'] == 1?'已绑定':'未绑定';
                $value['frozen_status'] = $value['frozen_flag'] == 1?'已冻结':'正常';
            }
        }

        return $data;

    }

    /**
     * @param Request $request
     * 添加账号
     */
    public function addAccount(Request $request)
    {

        $where = $request->except('_token');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $Account = new AccountService();

        $ret =  $Account->doAccountAdd($where);
        if($ret)
        {
            return $ret;
        }

    }

    /**
     * @param Request $request
     * 会员充值流水号
     */
    public function getAdminOrder(Request $request)
    {

        $where = [];

        $Account = new AccountService();

        $ret =  $Account->AdminOrder($where);

        if($ret)
        {
            return $ret;
        }

    }


    /**
     * @param Request $request
     * 会员充值
     */
    public function accRecharge(Request $request)
    {

        $where = $request->except('_token');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $Account = new AccountService();

        $ret =  $Account->doAccountRecharge($where);

        if($ret)
        {
            return $ret;
        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 冻结账号
     */
    public function forbiddenMem(Request $request)
    {

        $where = [];
        $where['u_id'] = trim($request->input('u_id'), ',');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));


        $Account = new AccountService();
        $data = $Account->doAccountForbidden($where);

        if ($data) {
            return $data;
        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 解冻账号
     */
    public function thawMember(Request $request)
    {

        $where = [];
        $where['u_id'] = trim($request->input('u_id'), ',');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));


        $Account = new AccountService();
        $data = $Account->doAccountThaw($where);

        if ($data) {
            return $data;
        }
    }


    /**
     * @param Request $request
     * 导出excel
     */
    public function downloadMemeberData(Request $request)
    {


        $where= $request->except('_token');

        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }

        $Account = new AccountService();
        $data = $Account->getAccountList($where);

        if($data && !empty($data['data']))
        {
            foreach ($data['data'] as &$value)
            {
                $value['frozen_status'] = $value['frozen_flag'] == 1?'已冻结':'正常';
                unset($value['wechat_bind']);
                unset($value['frozen_flag']);
            }

            $cellData = $data['data'];


            $title = ['会员ID','会员账号','手机号','钱包余额','用户来源','会员状态'];

            array_unshift($cellData,$title);

            Excel::create('会员列表',function($excel) use ($cellData){
                $excel->sheet('account', function($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            })->export('xlsx');

        }




    }







}